<?php
namespace app\index\controller;
use think\Controller;
use think\Request;
use think\Session;
use think\Db;
class Role extends \app\index\controller\Common
{
	public function lists()
	{
		$page=Request::instance()->get('page');
		$limit=Request::instance()->get('limit');
		//搜索条件
		$title=Request::instance()->get('title');
		$map = array();
		$admin_user = array();
		if ($title!="") {
			$map['title'] = ['like',"%$title%"];
		}
		
		$data=Db::table('auth_group')->where($map)->page("$page,$limit")->select();
		foreach ($data as $key => $value) {
			$data[$key]['rule_arr']=explode(',', $value['rules']);
		}
		$arr['code']='0';
		$arr['msg']='';
		$arr['count']=Db::table('auth_group')->where($map)->count();
		$arr['data']=$data;
		return $arr;
		//$this->result($admin_user,0,'获取成功');
	}
	public function role_list()
	{
		$map = array();
		$map['status']='1';
        $data=Db::table('auth_group')->where($map)->select();
        $this->result($data);
	}
	//验证超级管理员
	public function check_admin()
	{
		if (Request::instance()->has('password','post')) {
			$password=md5(Request::instance()->post('password'));
			$admin_user=Db::table('auth_group_access')
			->field('admin_user.id,admin_user.username,group.title')
			->alias('access')
			->join('auth_group group','access.group_id = group.id')
			->join('admin_user admin_user','admin_user.id = access.uid')
			->where('group.title','超级管理员')
			->where('password',$password)
			->find();
			if ($admin_user) {
				$this->result('','0','验证正确');
			}else{
				$this->result('','1','验证错误');
			}
		}
	}
	//添加
	public function add()
	{
		$data = array();
		$data['title']=Request::instance()->post('data.title');
		$data['rules']=Request::instance()->post('rules');
		if (Request::instance()->has('data.status','post')) {
			$data['status']=Request::instance()->post('data.status');
		}else{
			$data['status']='0';
		}
		Db::table('auth_group')->insert($data);
		$id = Db::name('auth_group')->getLastInsID();
		$this->result($id,0,'提交成功');
	}
	public function edit()
	{
		if (Request::instance()->has('data.id','post')) {
			$id=Request::instance()->post('data.id');

			$data = array();
			$data['title']=Request::instance()->post('data.title');
			$data['rules']=Request::instance()->post('rules');
			if (Request::instance()->has('data.status','post')) {
				$data['status']=Request::instance()->post('data.status');
			}else{
				$data['status']='0';
			}
			Db::table('auth_group')->where('id',$id)->update($data);
			$this->result($id,0,'提交成功');
		}
	}
	public function del()
	{
		if (Request::instance()->has('id','post')) {
			$id=Request::instance()->post('id');
			if (Db::table('auth_group')->delete($id)) {
				$this->result($id,0,'提交成功');
			}else{
				$this->result($id,1,'删除失败');
			}
		}
	}
}
